package com.dunqi.gpm.shunqing.home.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.dunqi.gpm.shunqing.R;
import com.dunqi.gpm.shunqing.common.api.ApiFactory;
import com.dunqi.gpm.shunqing.common.api.RxSchedulers;
import com.dunqi.gpm.shunqing.common.base.BaseAct;
import com.dunqi.gpm.shunqing.common.base.ComObserver;
import com.dunqi.gpm.shunqing.common.base.Data;
import com.dunqi.gpm.shunqing.common.view.ComTitleBar;
import com.dunqi.gpm.shunqing.home.constance.Constance;

import java.util.HashMap;

import butterknife.BindView;
import io.reactivex.disposables.Disposable;

public class InstructionsActivity extends BaseAct {


    private static final String REPLYPROINSTRUCTIONID = "replyProInstructionId";
    @BindView(R.id.title_bar)
    ComTitleBar titleBar;
    @BindView(R.id.edit)
    EditText edit;
    Long mainObjectId;
    private int msgType;
    private long instructionType;
    public static final String MSG_TYPE = "msgType";
    private long replyProInstructionId;

    @Override
    protected void refreshData(Bundle savedInstanceState) {

    }

    @Override
    protected void initUiAndListener() {
        mainObjectId = getIntent().getLongExtra(Constance.MAIN_OBJECT_ID, 0);
        instructionType= getIntent().getLongExtra(Constance.INSTRUCTION_TYPE, 0);
        replyProInstructionId = getIntent().getLongExtra(REPLYPROINSTRUCTIONID, 0);


        msgType = getIntent().getIntExtra(MSG_TYPE, 1);

        if (msgType == 1) {
            titleBar.setRightText("批示");
            titleBar.setTitle("批示与留言");
            edit.setHint("请在此输入新的工作批示");
        } else if (msgType == 2) {
            titleBar.setRightText("留言");
            titleBar.setTitle("批示与留言");
            edit.setHint("请在此输入新的留言");
        } else if (msgType == 3) {
            titleBar.setRightText("回复");
            titleBar.setTitle("回复");
            edit.setHint("请在此输入新的回复");
        } else {

        }

        titleBar.setOnClickListenerOfRight(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                commit();
            }
        });
    }

    //发布新批示
    private void commit() {
        String content = getContent();

        if (TextUtils.isEmpty(content)) {
            t("内容为空,请填写");
            return;
        }

        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("instructionType", String.valueOf(instructionType));
        hashMap.put("msgType", String.valueOf(msgType));
        hashMap.put("instructionObjId", String.valueOf(mainObjectId));
        hashMap.put("instructionText", content);
        hashMap.put("sendSmsNotice", "0");

        if (replyProInstructionId != 0) {
            hashMap.put("replyProInstructionId", String.valueOf(replyProInstructionId));
        }

        final ProgressDialog dialog = ProgressDialog.show(this, "提示", "正在提交...");

        ApiFactory.getInstance()
                .addInstruction(hashMap)
                .compose(RxSchedulers.<Data>io_main())
                .subscribe(new ComObserver() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposables(d);
                    }

                    @Override
                    public void onSuccess(Object o) {
                        dialog.dismiss();
                        t("提交成功");
                        setResult(200);
                        finish();
                    }

                    @Override
                    public void onFinal() {
                        dialog.dismiss();
                    }
                });
    }

    public String getContent() {
        return edit.getText().toString().trim();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_instructions;
    }

    public static void start(Activity context, Long mainObjectId,long instructionType, int msgType, Long replyProInstructionId) {
        Intent intent = new Intent(context, InstructionsActivity.class);
        intent.putExtra(Constance.MAIN_OBJECT_ID, mainObjectId);
        intent.putExtra(Constance.INSTRUCTION_TYPE, instructionType);
        intent.putExtra(MSG_TYPE, msgType);
        intent.putExtra(REPLYPROINSTRUCTIONID, replyProInstructionId);
        context.startActivityForResult(intent, 100);
    }
}
